package com.zhong.gang.applib.pop;

import android.content.Context;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.zhong.gang.applib.R;
import com.zhong.gang.applib.base.dialog.BasePop;
import com.zhong.gang.applib.databinding.PopTypeBinding;

import java.util.ArrayList;
import java.util.List;

public class TypePop extends BasePop<PopTypeBinding> implements BaseQuickAdapter.OnItemClickListener {
    private TypeAdapter adapter;
    private List<TypeBean> data = new ArrayList<>();
    private onTypeItemClickedListener listener;
    private View enterView;
    /***
     * 初始化数据
     * @param context
     */
    public TypePop(Context context,View view,List<TypeBean> data,onTypeItemClickedListener listener) {
        super(context);
        this.enterView = view;
        this.listener = listener;
        if (data == null)
            return;
        else this.data = data;
    }

    public List<TypeBean> getData() {
        return data;
    }

    @Override
    public int LayoutRes() {
        return R.layout.pop_type;
    }

    @Override
    public void initView() {
        bind.recyclerView.setLayoutManager(new LinearLayoutManager(context));
        bind.recyclerView.setAdapter(adapter = new TypeAdapter());
        adapter.setOnItemClickListener(this);
    }

    public void showAsDropDown(View anchor, int type) {
        if (getData().size() <= 1)
            return;
        super.showAsDropDown(anchor);
        if (adapter == null)
            return;
        List<TypeBean> adapterData = new ArrayList<>();
        for (TypeBean bean : data){
            if (type != bean.getType())
                adapterData.add(bean);
        }
        adapter.setNewData(adapterData);
    }



    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        TypeBean bean = (TypeBean) adapter.getItem(position);
        if (listener == null && this.enterView == null)
            return;
        listener.onTypeItem(this.enterView,bean);
        dismiss();
    }

    public interface onTypeItemClickedListener{
        void onTypeItem(View view,TypeBean typeBean);
    }
}
